/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package utfpr.cp.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

/**
 *
 * @author Adriano_2
 */
@Entity
public class HTMLTag implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    private Long id;
   
    @ManyToOne
    private URLData url;
    
    private String tagname;
    
    @ElementCollection  @Column(length = 1000)
    private Map<String, String> atributos = new HashMap<>();
    
    public HTMLTag() {
    }
    
    public HTMLTag(URLData url, String tagname) {
        this.url = url;
        setTagname(tagname);
    }

    @Override
    public String toString() {
        return "utfpr.cp.model.HTMLTag[ " + getTagname() + " ]";
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the url
     */
    public URLData getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(URLData url) {
        this.url = url;
    }

    /**
     * @return the tagname
     */
    public String getTagname() {
        return tagname;
    }

    /**
     * @param tagname the tagname to set
     */
    public void setTagname(String tagname) {
        this.tagname = tagname.trim();
        if (this.tagname.length() > 255) {
            this.tagname = this.tagname.substring(0, 252) + "...";
        }
    }

    public void addAttributos(String name, String value) {
        if (value.length() > 1000) {
            value = value.substring(0, 996) + "...";
        }
        name = name.trim();
        if (name.length() > 255) {
            name = name.substring(0, 252) + "...";
        }
        if (atributos.containsKey(name)) {
            atributos.put(name, atributos.get(name) + " " +value);
        }
        else {
            atributos.put(name, value);
        }
    }
    
    /**
     * @return the atributos
     */
    public Map<String, String> getAtributos() {
        return atributos;
    }

    /**
     * @param atributos the atributos to set
     */
    public void setAtributos(Map<String, String> atributos) {
        this.atributos = atributos;
    }
}
